#include"Udpserver.hpp"
#include"Comm.hpp"
#include<memory>
#include<stdio.h>

using namespace std;


string OnMessageDefault(string request)
{
    return request+"[haha, got you!!]";
}


string ExecuteCommand(string command)//我们的处理客户端发来的消息，不一定直接返回字符串，我们还可以让客户端输入shell命令，然后执行
{
    cout<<"get a message :"<<command<<endl;
    FILE* fp=popen(command.c_str(),"r");
    if(fp==nullptr)
    {
        return "execute error, reason is uknown";
    }

    string response;
    char buffer[1024]={0};
    while(true)
    {
        char* s=fgets(buffer,sizeof(buffer),fp);
        if(!s) break;//如果读取为空就返回
        else response+=buffer;
    }

    pclose(fp);
    return response;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        cout<<"Usage:\n     ./udp_echo_server <port>"<<endl;
        return -1;
    }

    // string ip=argv[1];
    uint16_t port=stoi(argv[1]);

    //unique_ptr<UdpServer> usvr=make_unique<UdpServer>();？？？
    //UdpServer* usvr=new UdpServer(OnMessageDefault,port);
    //UdpServer* usvr=new UdpServer(ExecuteCommand,port);
    UdpServer* usvr=new UdpServer(port);
    usvr->Init();
    usvr->Start();
    return 0;
}